package cultu.core.service.impl;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import cultu.core.dao.RoleDAO;
import cultu.core.message.Context;
import cultu.core.message.JpqlParameter;
import cultu.core.message.Role;
import cultu.core.message.RoleCriteria;
import cultu.core.message.RoleList;
import cultu.core.message.RoleScenario;
import cultu.core.service.RoleManager;

/**
 * The Class RoleManagerImpl.
 */
@Transactional
public class RoleManagerImpl implements RoleManager
{

  /** The role dao. */
  private RoleDAO roleDao;
  
  /**
   * Sets the role dao.
   *
   * @param value the new role dao
   */
  public void setRoleDao(RoleDAO value)
  {
    this.roleDao = value;
  }
  
  /**
   * @see cultu.core.service.RoleManager#getRole(cultu.core.message.Context, cultu.core.message.RoleScenario, cultu.core.message.Role)
   */
  public Role getRole(Context pContext, RoleScenario pScenario, Role pRole)
  {
    // TODO Auto-generated method stub
    return null;
  }

  /**
   * @see cultu.core.service.RoleManager#createRole(cultu.core.message.Context, cultu.core.message.RoleScenario, cultu.core.message.Role)
   */
  public Role createRole(Context pContext, RoleScenario pScenario, Role pRole)
  {
    // TODO Auto-generated method stub
    return null;
  }

  /**
   * @see cultu.core.service.RoleManager#deleteRole(cultu.core.message.Context, cultu.core.message.RoleScenario, cultu.core.message.Role)
   */
  public Role deleteRole(Context pContext, RoleScenario pScenario, Role pRole)
  {
    // TODO Auto-generated method stub
    return null;
  }

  /**
   * @see cultu.core.service.RoleManager#updateRole(cultu.core.message.Context, cultu.core.message.RoleScenario, cultu.core.message.Role)
   */
  public Role updateRole(Context pContext, RoleScenario pScenario, Role pRole)
  {
    // TODO Auto-generated method stub
    return null;
  }

  /**
   * @see cultu.core.service.RoleManager#findRoles(cultu.core.message.Context, cultu.core.message.RoleScenario, cultu.core.message.RoleCriteria)
   */
  @Transactional(readOnly=true)
  public RoleList findRoles(Context pContext, RoleScenario pScenario, RoleCriteria pCriteria)
  {
    RoleList result = new RoleList();

    if (pCriteria != null) {
      List<Role> myList = roleDao.find(pCriteria, pScenario);
      result.getRole().addAll(myList);
    }

    return result;
  }

  /**
   * @see cultu.core.service.RoleManager#deleteRoles(cultu.core.message.Context, cultu.core.message.RoleScenario, cultu.core.message.RoleList)
   */
  public RoleList deleteRoles(Context pContext, RoleScenario pScenario, RoleList pList)
  {
    // TODO Auto-generated method stub
    return null;
  }

  /**
   * @see cultu.core.service.RoleManager#createRoles(cultu.core.message.Context, cultu.core.message.RoleScenario, cultu.core.message.RoleList)
   */
  public RoleList createRoles(Context pContext, RoleScenario pScenario, RoleList pList)
  {
    // TODO Auto-generated method stub
    return null;
  }

  /**
   * @see cultu.core.service.RoleManager#updateRoles(cultu.core.message.Context, cultu.core.message.RoleScenario, cultu.core.message.RoleList)
   */
  public RoleList updateRoles(Context pContext, RoleScenario pScenario, RoleList pList)
  {
    // TODO Auto-generated method stub
    return null;
  }
  
  /**
   * {@inheritDoc}
   */
  public Role getRoleByCode(final String code) {

    // The criteria.
    RoleCriteria roleCriteria = new RoleCriteria();

    roleCriteria.setJpqlQuery("Select r From Role r Where r.code = :code");
    JpqlParameter parameter = new JpqlParameter();
    parameter.setAlias("code");
    parameter.setValue(code);
    roleCriteria.getParameter().add(parameter);

    // The scenario.
    final RoleList rolesList = findRoles(null, new RoleScenario(), roleCriteria);

    if (rolesList.getRole().isEmpty()) {
      return null;
    } else {
      return rolesList.getRole().get(0);
    }
  }
  
}
